
var app= angular.module('user_6App', []);
app.controller('user_6Controller', function($scope) {
	$scope.id = 888;
	var roles=['mamaster,'+$scope.id,'proposer,'+$scope.id,'involver,'+$scope.id,'recorder,'+$scope.id];
	var connections={};
	var role='mamaster,'+$scope.id;
	$scope.socket = new WebSocket("ws://" + window.location.host +"/webSocket/meeting/" + role);
	var memberin;
	//处理到来的信令
	$scope.socket.onmessage = function(event) {
		var json = JSON.parse(event.data);	
        if(json.open_rep!=null){}else if(json.proCloseOwn!=null){
			myVid=document.getElementById("proposer,"+$scope.id+"Video");
			myVid.muted=true; 
		}else if(json.proOpenOwn!=null){
			myVid=document.getElementById("proposer,"+$scope.id+"Video");
			myVid.muted=false; 
		}else if(json.repCloseOwn!=null){ 
			myVid=document.getElementById("involver,"+$scope.id+"Video");
			myVid.muted=true; 	
		}else if(json.repOpenOwn!=null){
			myVid=document.getElementById("involver,"+$scope.id+"Video");
			myVid.muted=false; 	
		}else if(json.recCloseOwn!=null){
			myVid=document.getElementById("recorder,"+$scope.id+"Video");
			myVid.muted=true;  
		}else if(json.recOpenOwn!=null){
			myVid=document.getElementById("recorder,"+$scope.id+"Video");
			myVid.muted=false;  
		}else if(json.event=="role"){
			var role_to=json.data;
			con=connections[role_to];
			con.pc.close();
			
		}else{  
			console.log("使用websokect传输在线视频信息")
            var con;
			//发送对象
			if(connections[json.owner])con=connections[json.owner];
			
			//如果是一个ICE的候选，则将其加入到PeerConnection中，否则设定对方的session描述为传递过来的描述
			if (json.event==="msg") {
				if(json.data!=role){
					con=new Connection(role,$scope.socket);	
					con.to=json.data;
					con.pc.addStream($scope.stream);
					connections[json.data]=con;
				}
				
	
			} else if(json.event === "_ice_candidate"){
				con.pc.addIceCandidate(new RTCIceCandidate(json.data.candidate));	
			}else if(json.event==="rolelist"){
				memberin=json.data;
				for(var i=0;i<memberin.length;i++){
					if(memberin[i]==role)continue;
					var con=new Connection(role,$scope.socket);
					con.to=memberin[i];
					connections[con.to]=con;
					
				}
			}
			else{
				con.pc.setRemoteDescription(new RTCSessionDescription(
						json.data.sdp));
				// 如果是一个offer，那么需要回复一个answer
				if (json.event === "_offer") {
					//_offer
					con.pc.createAnswer(con.sendAnswerFn, function(error) {
					});
				} else {
					//_answer
				}
			}				
		}

	};
	



	// 获取本地音频和视频流
    navigator.getUserMedia({
        audio: true,
        video: true
    }, function(stream){
        //绑定本地媒体流到video标签用于输出
        document.getElementById(role+'Video').src = window.URL.createObjectURL(stream);
        //关闭自己回声的话筒
        myVid=document.getElementById(role+"Video");
    	myVid.muted=true;
    	$scope.stream=stream;
        for(var i=0;i<roles.length;i++){
        	if(!connections[roles[i]])continue;
        	var con=connections[roles[i]];
            //向PeerConnection中加入需要发送的流
            con.pc.addStream(stream);
            //如果是发起方则发送一个offer信令
            if(memberin.indexOf(con.to)>=0){
            	con.start();
            }            	
        }
    }, function(error){
        //处理媒体流创建失败错误
    });
    

})